package com.hhf.feiojuserservice.controller.inner;

import com.hhf.feiojapiservice.client.UserFeignClient;
import com.hhf.feiojcommonservice.common.ErrorCode;
import com.hhf.feiojcommonservice.exception.BusinessException;
import com.hhf.feiojcommonservice.model.entity.User;
import com.hhf.feiojcommonservice.utils.UserIdContext;
import com.hhf.feiojuserservice.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.List;

/**
 * @Author hhf
 * @Date 2024/11/3
 * @Description
 */
@RestController
@RequestMapping("/inner")
public class UserInnerController implements UserFeignClient {

    @Resource
    private UserService userService;

    @Override
    public User getLoginUser(HttpServletRequest request) {

        Long userId = UserIdContext.getUserId();

        // 先判断是否已登录
        if (userId == null ) {
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
        }

        User user = userService.getById(userId);

        return user;
    }

    @Override
    public User getCurrentUser() {
        Long userId = UserIdContext.getUserId();

        // 先判断是否已登录
        if (userId == null ) {
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
        }

        User user = userService.getById(userId);

        return user;
    }

    @Override
    @GetMapping("/get/ids")
    public List<User> listByIds(Collection<Long> idList) {
        return userService.listByIds(idList);
    }
}
